<template>
  <a-card :bordered="false">
    <!--  #####################查询条件###################  -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="商户名称">
              <a-input v-model="queryParam.name" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="客户(商圈)">
              <a-select v-model="queryParam.parentCode" allowClear>
                <a-select-option v-for="item in companys" :key="item.code">{{item.name}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons"
                  :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button v-action:query type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!--  #####################查询条件###################  -->

    <!--  #####################操作###################  -->
    <div class="table-operator">
      <a-button v-action:add type="primary" icon="plus" @click="$refs.createModal.add()">新建</a-button>
      <!--<a-button v-action:delete type="danger" icon="delete" @click="$refs.createReset.add()">清零真实收款金额</a-button>-->
    </div>
    <!--  #####################操作###################  -->

    <!--  #####################表格###################  -->
    <s-table
      ref="table"
      :columns="columns"
      :data="loadData"
      bordered
    >
      <span slot="fileUrl" slot-scope="text, record">
        <img :src="text" alt="" height="100px">
      </span>
      <span slot="fileUrl2" slot-scope="text, record">
        <img :src="text" alt="" height="100px">
      </span>
      <span slot="isValid" slot-scope="text">
        <a-tag v-if="text==false" color="red">
             <b>{{ text | statusFilter }}</b>
        </a-tag>
        <a-tag v-if="text==true" color="green">
             <b>{{ text | statusFilter }}</b>
        </a-tag>
      </span>
      <span slot="usableAmount" slot-scope="text">
         <b>￥{{ text * 0.01 }}元</b>
      </span>

      <span slot="realAmount" slot-scope="text">
         <b>￥{{ text * 0.01 }}元</b>
      </span>

      <span slot="action" slot-scope="text, record">
        <template>
          <a v-action:edit @click="handleEdit(record)">编辑</a>
          <a-divider v-action:edit type="vertical" />
          <a v-action:delete @click="handleDel(record)">删除</a>
        </template>
      </span>
    </s-table>
    <!--  #####################表格###################  -->

    <!--  #####################打开的页面###################  -->
    <merchant-edit ref="createModal" @ok="handleOk"/>
    <merchant-reset ref="createReset" @ok="handleOk"/>
    <!--  #####################打开的页面###################  -->
  </a-card>
</template>

<script>
  import moment from 'moment'
  import { STable } from '@/components'
  import merchantEdit from './merchantEdit'
  import merchantReset from './merchantReset'
  import { selectAllCustomer } from '@/api/shop/customer'
  import { selectPage, del } from '@/api/shop/merchant'

  export default {
    name: 'menuList',
    components: {
      STable,
      merchantEdit,
      merchantReset
    },
    data () {
      return {
        mdl: {},
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        companys: [],
        // 表头
        columns: [
          {
            title: '商户logo图片',
            dataIndex: 'fileUrl',
            scopedSlots: { customRender: 'fileUrl' }
          },
          {
            title: '商户主图',
            dataIndex: 'fileUrl2',
            scopedSlots: { customRender: 'fileUrl2' }
          },
          {
            title: '商户名称',
            dataIndex: 'name'
          },
          {
            title: '排序',
            dataIndex: 'sortNo'
          },
          {
            title: '微信子商户号',
            dataIndex: 'subMchId'
          },
          {
            title: '子商户appid',
            dataIndex: 'subAppid'
          },
          /*{
            title: '支付宝商户号',
            dataIndex: 'aliMchId'
          },
          {
            title: '支付宝授权TOKEN',
            dataIndex: 'aliAuthToken'
          },*/
          /*{
            title: '真实收款金额',
            dataIndex: 'realAmount',
            scopedSlots: { customRender: 'realAmount' }
          },
          {
            title: '可接收金额',
            dataIndex: 'usableAmount',
            scopedSlots: { customRender: 'usableAmount' }
          },
          {
            title: '是否可用',
            dataIndex: 'isValid',
            scopedSlots: { customRender: 'isValid' }
          },*/
          {
            title: '操作',
            dataIndex: 'action',
            width: '150px',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          //查询代理商
          this.queryParam.type = 2
          return selectPage(Object.assign(parameter, this.queryParam))
            .then(res => {
              return res.result
            })
        }
      }
    },
    filters: {
      statusFilter(state) {
        const statusMap = {
          false: '不可用',
          true: '可用'
        }
        return statusMap[state]
      }
    },
    created() {
      selectAllCustomer().then(res => {
        this.companys = res.result
      })
    },
    methods: {
      handleEdit (record) {
        this.$refs.createModal.edit(record)
      },

      handleDel (record) {
        const _this = this
        this.$confirm({
          title: '警告',
          content: `真的要删除 ${record.name} 吗?`,
          okText: '删除',
          okType: 'danger',
          cancelText: '取消',
          onOk () {
            // 在这里调用删除接口
            del(record.id).then(res => {
              if (res.code === 200) {
                _this.$message.success(res.message)
                _this.$refs.table.refresh()
              }
              if (res.code === 500) {
                _this.$message.error(res.message)
              }
            })
          }
        })
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      resetSearchForm () {
        this.queryParam = {
          date: moment(new Date())
        }
      }
    }
  }
</script>
